<!-- 图片预览 -->
<template>
  <div class="CustomImagePreview-root" ref="full_image_box">
    <div class="image-preview-mask" @click="handleExitFullScreen"></div>
    <div class="image-preview-body">
      <div class="image-preview-operations">
        <div class="btn-box" @click="handleExitFullScreen">
          <a-icon class="image-preview-operations-icon" type="close" />
        </div>
        <div class="btn-box" @click="handleZoomin">
          <a-icon class="image-preview-operations-icon" type="zoom-in" />
        </div>
        <div class="btn-box" :class="{ 'operation-disabled': scale === 1 }" @click="handleZoomout">
          <a-icon class="image-preview-operations-icon" type="zoom-out" />
        </div>
      </div>
      <div class="image-preview-img-wrapper">
        <img class="image-preview-img" :src="src" :style="`transform: scale3d(${scale}, ${scale}, 1);`" />
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: 'CustomImagePreview',
  data() {
    return {
      src: '',
      scale: 1,
    };
  },
  methods: {
    handleExitFullScreen() {
      this.$destroy();
    },
    handleZoomin() {
      this.scale = this.scale + 1;
    },
    handleZoomout() {
      if (this.scale === 1) return;
      this.scale = this.scale - 1;
    },
  },
  beforeDestroy() {
    const body = document.getElementsByTagName('body')[0];
    body.removeChild(this.$refs.full_image_box);
  },
};
</script>

<style lang="scss" scoped>
.CustomImagePreview-root {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  z-index: 1001;
  .image-preview-mask {
    position: fixed;
    top: 0;
    right: 0;
    bottom: 0;
    left: 0;
    z-index: 1000;
    height: 100%;
    background-color: rgba(0, 0, 0, 0.45);
  }
  .image-preview-body {
    pointer-events: none;
    overflow: hidden;
    z-index: 1080;
    position: fixed;
    top: 0;
    right: 0;
    bottom: 0;
    left: 0;
    .image-preview-img-wrapper {
      position: absolute;
      top: 0;
      right: 0;
      bottom: 0;
      left: 0;
      transition: transform 0.3s cubic-bezier(0.215, 0.61, 0.355, 1) 0s;
      display: flex;
      align-items: center;
      justify-content: center;
      .image-preview-img {
        max-width: 100%;
        max-height: 100%;
        vertical-align: middle;
        transform: scale3d(1, 1, 1);
        transition: transform 0.3s cubic-bezier(0.215, 0.61, 0.355, 1) 0s;
        user-select: none;
        pointer-events: auto;
      }
    }
    .image-preview-operations {
      position: absolute;
      top: 0;
      right: 0;
      z-index: 1;
      display: flex;
      flex-direction: row-reverse;
      align-items: center;
      width: 100%;
      color: rgba(255, 255, 255, 0.85);
      background: rgba(0, 0, 0, 0.1);
      pointer-events: auto;
      font-size: 14px;
      height: 46px;
      .btn-box {
        margin-left: 12px;
        padding: 12px;
        cursor: pointer;
        &.operation-disabled {
          color: rgba(255, 255, 255, 0.45);
          pointer-events: none;
        }
      }
      .image-preview-operations-icon {
        font-size: 18px;
      }
    }
  }
}
</style>
